package com.somsoft.handjc.activity.rank.xuncha;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import com.somsoft.handjc.activity.MainActivity;
import com.somsoft.handjc.activity.R;
import com.somsoft.handjc.util.MyApplication;
import com.somsoft.handjc.util.Utils;
import com.somsoft.handjc.util.XunChaDataUtil;

/**
 * 实现功能: 
 * 1.使用TableLayout动态布局展示,可动态添加和删除.
 * 2.初始化时显示动态展示,初始化的数据改造后可来自数据库. 
 * 3.重置时到初始化状态.
 * 4.保存时去重检查,参见代码中去重算法. 
 * 
 */
public class JiXiaoRankActivity extends Activity {

	private TableLayout table = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MyApplication.getInstance().addActivity(this);
		setContentView(R.layout.xuncha_jx_rank);
		setTitle("效能巡查—绩效排名");
		table = (TableLayout) findViewById(R.id.dictTable);

		initDictItemTable();
		Utils.closeLoading();
	}

	public void initDictItemTable() {
		
		try {
			
			Intent it = getIntent();
        	List<NameValuePair> params = new ArrayList<NameValuePair>();
        	final String strMonth = it.getStringExtra("strMonth");
        	
        	if(strMonth!=null && !"".equals(strMonth)){
				params.add(new BasicNameValuePair("xuncha_rank_month",formatDate(strMonth)));
			}
        	
			JSONArray array = XunChaDataUtil.getJiXiaoRank(params);
			XunChaDataUtil.rankData = array;
			
			for (int i = 0; i < array.length(); i++) {
				
				TableRow row = new TableRow(this);
				final JSONObject obj = array.getJSONObject(i);
				
				TextView rank = new TextView(this);
				rank.setText(obj.getString("rank"));
				rank.setGravity(Gravity.CENTER);
				rank.setTextSize(20);

				TextView deptName = new TextView(this);
				deptName.setText(obj.getString("deptName"));
				deptName.setGravity(Gravity.CENTER);
				deptName.setTextSize(20);

				TextView totalScore = new TextView(this);
				totalScore.setText(obj.getString("totalScore"));
				totalScore.setGravity(Gravity.CENTER);
				totalScore.setTextSize(20);

				Button opr = new Button(this);
				opr.setText("详情");
				opr.setGravity(Gravity.CENTER);
				opr.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						try {
							Utils.openLoading(JiXiaoRankActivity.this);
							Intent it = new Intent(JiXiaoRankActivity.this,JiXiaoRankDetailActivity.class);
							it.putExtra("deptName", obj.getString("deptName"));
							startActivity(it);
						} catch (Exception e) {
							Toast.makeText(JiXiaoRankActivity.this, "加载数据失败！", Toast.LENGTH_SHORT).show();
						}
						
					}
				});
				
				row.addView(rank);
				row.addView(deptName);
				row.addView(totalScore);
				row.addView(opr);

				table.addView(row, new TableLayout.LayoutParams());
			}
		} catch (Exception e) {
			Toast.makeText(JiXiaoRankActivity.this, "加载数据失败！", Toast.LENGTH_SHORT).show();
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {			
		menu.add(Menu.NONE, 10, 10, "首页").setIcon(android.R.drawable.ic_menu_compass);
		menu.add(Menu.NONE, 20, 20, "查询").setIcon(android.R.drawable.ic_menu_search);
		menu.add(Menu.NONE, 90, 90, "退出").setIcon(android.R.drawable.ic_menu_close_clear_cancel);
		return true;											
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {		// 选中某个菜单项
		Intent it = null;
		switch (item.getItemId()) {								// 判断菜单项ID
		case  10:
			it = new Intent(JiXiaoRankActivity.this,MainActivity.class);
			startActivity(it);
			break;
		case  20:
			LayoutInflater factory = LayoutInflater.from(this) ;
			View myView = factory.inflate(R.layout.xuncha_rank_query, null) ;
			final EditText month = (EditText)myView.findViewById(R.id.xuncha_rank_month);
			
			
			Dialog dialog = new AlertDialog.Builder(this)
				.setIcon(android.R.drawable.ic_menu_search) 
				.setTitle("请选择月份")
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						try {
							String strMonth = month.getText().toString();
							Intent it = new Intent(JiXiaoRankActivity.this,JiXiaoRankActivity.class);
							it.putExtra("strMonth", strMonth);
							startActivity(it);
						} catch (Exception e) {
							Log.e("JiXiaoRankActivity", e.getMessage(),e);
						}
					}
				})
				.setNegativeButton("取消",new DialogInterface.OnClickListener(){
					@Override
					public void onClick(DialogInterface dialog, int which) {
						try {
							dialog.dismiss();
						} catch (Exception e) {
							Log.e("JiXiaoRankActivity", e.getMessage(),e);
						}
					}
					
				}).setView(myView).create();
			dialog.show() ;
			break;
		case  90:
			MyApplication.getInstance().exit();
			break;
		}
		return false;
	}
	
	private String formatDate(String date){
		//201208转换为2012-08
		if("".equals(date)){
			return "";
		}
		String temp = "";
		temp += date.substring(0,4)+"-";
		temp += date.substring(4,6);
		return temp;
	}
}
